<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplier">
              <a-input placeholder="请输入单据编号" style="width: 100%" v-model="queryParam.receiptsNo"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item label="单据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplier">
              <a-select
                :allowClear="true"
                placeholder="请选择单据类型"
                v-model="queryParam.receiptsType"
                style="width: 100%"
                @change="changeReceiptsType">
                <a-select-option value="1">采购订单</a-select-option>
                <a-select-option value="2">采购入库单</a-select-option>
                <a-select-option value="3">采购退货单</a-select-option>
                <a-select-option value="4">其他入库单</a-select-option>
                <a-select-option value="5">其他出库单</a-select-option>
                <a-select-option value="6">调拨订单</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depositoryName">
              <a-select
                :allowClear="true"
                placeholder="请选择"
                v-model="queryParam.approveStatus"
                style="width: 100%"
                @change="changeApproveStatus">
                <a-select-option value="1">待审批</a-select-option>
                <a-select-option value="2">已审批</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="4">
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <a slot="receiptsNo" @click="orderItemDetail(record)" slot-scope="text,record">{{ text }}</a>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定同意本次审批吗？" @confirm="() => approve(record,3)">
            <a v-if="record.approveStatus == 1">同意</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="record.approveStatus == 1"/>
          <a-popconfirm title="确定拒绝本次审批吗?" @confirm="() => approve(record,4)">
            <a v-if="record.approveStatus == 1">拒绝</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <purchase-order-detail-modal ref="purchaseOrderDetailModal"></purchase-order-detail-modal>
    <in-depository-order-detail-modal ref="inDepositoryOrderDetailModal"></in-depository-order-detail-modal>
    <goods-rejected-order-detail-modal ref="goodsRejectedOrderDetailModal"></goods-rejected-order-detail-modal>
    <other-in-depository-order-detail-modal ref="otherInDepositoryOrderDetailModal"></other-in-depository-order-detail-modal>
    <other-out-depository-order-detail-modal ref="otherOutDepositoryOrderDetailModal"></other-out-depository-order-detail-modal>
    <allocate-and-transfer-detail-modal ref="allocateAndTransferDetailModal"></allocate-and-transfer-detail-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {getAction, putAction} from "../../../../api/manage";
  import purchaseOrderDetailModal from "../purchaseOrder/modules/purchaseOrderDetailModal";
  import inDepositoryOrderDetailModal from "../purchaseInDepositoryOrder/modules/inDepositoryOrderDetailModal";
  import goodsRejectedOrderDetailModal from "../purchaseGoodsRejectedOrder/modules/goodsRejectedOrderDetailModal";
  import otherInDepositoryOrderDetailModal from "../otherInDepositoryOrder/modules/otherInDepositoryOrderDetailModal";
  import otherOutDepositoryOrderDetailModal from "../otherOutDepositoryOrder/modules/otherOutDepositoryOrderDetailModal";
  import allocateAndTransferDetailModal from "../allocateAndTransfer/modules/allocateAndTransferDetailModal";

  export default {
    name: 'approveList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      purchaseOrderDetailModal,
      inDepositoryOrderDetailModal,
      goodsRejectedOrderDetailModal,
      otherInDepositoryOrderDetailModal,
      otherOutDepositoryOrderDetailModal,
      allocateAndTransferDetailModal,
    },
    data () {
      return {
        description: '采购审批表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'receiptsNo',
            slots: { title: 'customTitle' },
            scopedSlots: { customRender: 'receiptsNo' },
          },
          {
            title:'单据类型',
            align:"center",
            dataIndex: 'receiptsType',
            customRender:function (text) {
              switch (text) {
                case 1:
                  return "采购"
                  break;
                case 2:
                  return "采购入库"
                  break;
                case 3:
                  return "采购退货"
                  break;
                case 4:
                  return "其他入库"
                  break;
                case 5:
                  return "其他出库"
                  break;
                case 6:
                  return "调拨单"
                  break;
              }
            }
          },
          {
            title:'采购员',
            align:"center",
            dataIndex: 'purchasePeople'
          },
          {
            title:'金额',
            align:"center",
            dataIndex: 'price'
          },
          {
            title:'提交人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'提交时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title:'审批状态',
            align:"center",
            dataIndex: 'approveStatus',
            customRender:function (text) {
              return text == 1?"待审批":"已审批"
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ghSupplychainPurchaseApprove/list",
          delete: "/ghSupplychainPurchaseApprove/delete",
          deleteBatch: "/ghSupplychainPurchaseApprove/deleteBatch",
          exportXlsUrl: "/ghSupplychainPurchaseApprove/exportXls",
          importExcelUrl: "/ghSupplychainPurchaseApprove/importExcel",
          edit: "/ghSupplychainPurchaseApprove/edit",
          queryPurchaseOrder: "/ghSupplychainPurchaseOrder/queryPurchaseByOrderNo",
          otherInDepositoryOrder: "/ghSupplychainOtherInDepositoryOrder/queryOtherInDepositoryOrderByOrderNo",
          otherOutDepositoryOrder: "/ghSupplychainOtherOutDepositoryOrder/queryOtherOutDepositoryOrderByOrderNo",
          allocateOrder: "/ghSupplychainAllocateAndTransfer/queryAllocateByOrderNo",
        },
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {

    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      orderItemDetail(record){
        switch (record.receiptsType) {
          case 1:
            this.$refs.purchaseOrderDetailModal.title = "采购订单详情";
            getAction(this.url.queryPurchaseOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.purchaseOrderDetailModal.showDetail(obj);
                }
              }
            })
            break;
          case 2:
            this.$refs.inDepositoryOrderDetailModal.title = "采购入库单详情";
            getAction(this.url.queryPurchaseOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.inDepositoryOrderDetailModal.showDetail(obj);
                }
              }
            })
            break;
          case 3:
            this.$refs.goodsRejectedOrderDetailModal.title = "采购退货订单详情";
            getAction(this.url.queryPurchaseOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.goodsRejectedOrderDetailModal.showDetail(obj);
                }
              }
            })
            break;
          case 4:
            this.$refs.otherInDepositoryOrderDetailModal.title = "其他入库单详情";
            getAction(this.url.otherInDepositoryOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.otherInDepositoryOrderDetailModal.showDetail(obj);
                }
              }
            })
            break;
          case 5:
            this.$refs.otherOutDepositoryOrderDetailModal.title = "其他出库单详情";
            getAction(this.url.otherOutDepositoryOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.otherOutDepositoryOrderDetailModal.showDetail(obj);
                }
              }
            })
            break;
          case 6:
            this.$refs.allocateAndTransferDetailModal.title = "调拨单详情";
            getAction(this.url.allocateOrder,{orderNo:record.receiptsNo}).then((res) => {
              if (res.success){
                var obj = res.result;
                if (obj == null){
                  this.$message.error("当前订单不存在")
                  return;
                }else {
                  this.$refs.allocateAndTransferDetailModal.showDetail(obj);
                }
              }
            })
            break;
        }
      },
      approve(record,result){
        var that = this;
        let params = {};
        params.id = record.id;
        params.purchaseId = record.purchaseId;
        params.receiptsNo = record.receiptsNo;
        params.approveResult = result;
        params.approveStatus = 2;
        params.receiptsType = record.receiptsType;
        putAction(this.url.edit,params).then(res =>{
          if (res.success){
            that.$message.success(res.message)
            that.loadData();
          }else {
            that.$message.warning(res.message)
          }
        })
      },
      changeReceiptsType(value){
        this.loadData(1);
      },
      changeApproveStatus(value){
        this.loadData(1);
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>